package models;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import objects.Concept;
import objects.Constituent;
import objects.Feature;
import objects.Features;
import objects.Translation;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/* Accessible only through SemRepModel */
class SemRepXMLReader {
	
	private Constituent con;
	
	public SemRepXMLReader() {
		con = null;
	}
	
	public void read(File file) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		Document doc = docBuilder.parse(file);
		
		con = (Constituent)getXML(doc.getDocumentElement());
	}
	
	public Object getXML(Node node) {
		Object created = null;
		
		if (node.getNodeName() == "constituent") {
			created = new Constituent();
			
			Attr label = (Attr) node.getAttributes().getNamedItem("label");
			Attr concept = (Attr) node.getAttributes().getNamedItem("concept");
			Attr sense = (Attr) node.getAttributes().getNamedItem("sense");
			Attr translation = (Attr) node.getAttributes().getNamedItem("translation");
			
			if (label != null) {
				((Constituent) created).setLabel(label.getValue());
			}
			if (concept != null) {
				((Constituent) created).setConcept(new Concept(concept
						.getValue()));
			}
			if (sense != null) {
				((Constituent) created).setSense(sense.getValue());
			}
			if (translation != null) {
				((Constituent) created).setTranslation(new Translation(translation.getValue()));
			}
		}
		
		else if (node.getNodeName() == "features") {
			created = new Features();
		}
		
		else if (node.getNodeName() == "feature") {
			created = new Feature();
			((Feature) created).setValueList(null);
			
			Attr name = (Attr) node.getAttributes().getNamedItem("name");
			Attr value = (Attr) node.getAttributes().getNamedItem("value");
			
			if (name != null) {
				((Feature) created).setName(name.getValue());
			}
			
			if (value != null) {
				((Feature) created).setValue(value.getValue());
			}
		}

		if (node.hasChildNodes()) {
			for (int i = 0; i < node.getChildNodes().getLength(); i++) {
				Object returnedType = getXML(node.getChildNodes().item(i));

				if (returnedType != null) {
					if (returnedType.getClass().getName() == "objects.Constituent") {
						((Constituent) created)
								.addConstituentListItem((Constituent) returnedType);
						((Constituent) returnedType).setParent((Constituent) created);
					} else if (returnedType.getClass().getName() == "objects.Features") {
						((Constituent) created)
								.setFeatures((Features) returnedType);
					} else if (returnedType.getClass().getName() == "objects.Feature") {
						((Features) created)
								.addFeatureListItem((Feature) returnedType);
					}
				}
			}
		}

		return created;
	}
	
	public Constituent getConstituent(){
		return con;
	}
	
}